﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;
using Controller;

using DevExpress.XtraTreeList.Nodes;
using Common;
using Model;
using System.Drawing.Design;
using PPCodes.ToolBoxPad;
using System.Xml;
using PPCodes.Log;
using System.Diagnostics;



namespace CodeFactory
{
    public partial class MainStudio : Form
    {

        #region 初始化

        /// <summary>
        /// 初始化各个功能窗口
        /// </summary>
        private void InitializeCustomWindow()
        {
            serverExplorerFM = new ServerExplorer(this);
            //serverExplorerFM.Parent = this;
            serverExplorerFM.HideOnClose = true;

            CSharpSourceFM = new CSharpSourceForm();
            //CSharpSourceFM.Parent = this;
            CSharpSourceFM.HideOnClose = true;

            CSharpSourceDG = new CSharpSourceDesign();
            //CSharpSourceFM.Parent = this;
            CSharpSourceDG.HideOnClose = true;

            xmlSourceFM = new XMLSourceForm();
            //xmlSourceFM.Parent = this;
            xmlSourceFM.HideOnClose = true;

            conSideBar = new ControlsSideBar();
            //conSideBar.Parent = this;
            conSideBar.HideOnClose = true;

            designFM = new DesignForm(CSharpSourceFM, xmlSourceFM, CSharpSourceDG);
            //designFM.Parent = this;
            designFM.HideOnClose = true;

            
            newProjectFM = new NewProjectFM(this);
            newProjectFM.Visible = false;

        }


        public MainStudio()
        {
            InitializeComponent();

            //SkinInitialize();

            deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);

            InitializeCustomWindow();

            SidebarEventBind();

        }


        private void MainStudio_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = String.Format("{0} {1}","CodeFactory --Build",PubConstant.Version);
                dockPanelMain.ShowDocumentIcon = true;
                LoadMainWindowDockingConfig();
                conSideBar.Hide();//没有工程的时候隐藏工具栏
            }
            catch (Exception ex)
            {
                FileLogger.Instance.ToInfo(ex.Message);
                throw ex;
            }
        }
        #endregion
        

        #region 私有字段

        //Sunisoft.IrisSkin.SkinEngine se = null;

        private DeserializeDockContent deserializeDockContent;
        private ServerExplorer serverExplorerFM;
        private DesignForm designFM;
        private CSharpSourceForm CSharpSourceFM;
        private CSharpSourceDesign CSharpSourceDG;
        private XMLSourceForm xmlSourceFM;
        private ControlsSideBar conSideBar;
        private exFullInfoForBuild exInfo;
        private bool m_bSaveLayout = true;
        private NewProjectFM newProjectFM;
        private string pathProject = "";
        #endregion
        
        
        #region 私有方法
       
        /// <summary>
        /// 初始化皮肤
        /// </summary>
        //private void SkinInitialize()
        //{
        //    se = new Sunisoft.IrisSkin.SkinEngine();
        //    se.SkinAllForm = true;
        //    se.SkinFile = "Skins\\DeepCyan.ssk";
        //}

         #region UI_Docking方法
        /// <summary>
        /// Dock方法
        /// </summary>
        /// <param name="persistString"></param>
        /// <returns></returns>
        private IDockContent GetContentFromPersistString(string persistString)
        {

            if (persistString == typeof(ServerExplorer).ToString())
              { 
                return serverExplorerFM; 
              }
            else if (persistString == typeof(DesignForm).ToString())
              {
                return designFM;
              }
            else if (persistString == typeof(CSharpSourceForm).ToString())
              {
                return CSharpSourceFM;
              }
            else if (persistString == typeof(CSharpSourceDesign).ToString())
            {
                return CSharpSourceDG;
            }
            else if (persistString == typeof(XMLSourceForm).ToString())
              {
                return xmlSourceFM;
              }
            else if (persistString == typeof(ControlsSideBar).ToString())
            {
                return conSideBar;
            }
            else
            {
                return null;
            }

            #region 参考代码
            //else
            //{
            // DummyDoc overrides GetPersistString to add extra information into persistString.
            // Any DockContent may override this value to add any needed information for deserialization.

            //    string[] parsedStrings = persistString.Split(new char[] { ',' });
            //    if (parsedStrings.Length != 3)
            //        return null;

            //    if (parsedStrings[0] != typeof(DummyDoc).ToString())
            //        return null;

            //    DummyDoc dummyDoc = new DummyDoc();
            //    if (parsedStrings[1] != string.Empty)
            //        dummyDoc.FileName = parsedStrings[1];
            //    if (parsedStrings[2] != string.Empty)
            //        dummyDoc.Text = parsedStrings[2];

            //    return dummyDoc;
            //}
            #endregion
            
        }

        /// <summary>
        /// 保存当前的主窗口的各个子窗口浮动位置设置
        /// </summary>
        private void SaveMainWindowDockingConfig()
        {
            string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
            if (m_bSaveLayout)
            {
                dockPanelMain.SaveAsXml(configFile);
            }
            else if (File.Exists(configFile))
            {
                File.Delete(configFile);
            }
            
        }

        /// <summary>
        /// 从配置文件读取主窗口的各个子窗口浮动位置设置
        /// </summary>
        private void LoadMainWindowDockingConfig()
        {
            string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
            if (File.Exists(configFile))
            {
                dockPanelMain.LoadFromXml(configFile, deserializeDockContent);
            }
        }

        /// <summary>
        /// 改变窗口状态（显示/隐藏）
        /// </summary>
        /// <param name="enumWT"></param>
        private void SetWindowShowOrHideState(enumWindowType enumWT)
        {
            switch (enumWT)
            {
                case enumWindowType.ControlSideBar:
                    if (conSideBar.IsHidden)
                    {
                        conSideBar.Show(dockPanelMain);
                        menuItemControlsSideBar.CheckState = CheckState.Checked;
                    }
                    else
                    {
                        conSideBar.Hide();
                        menuItemControlsSideBar.CheckState = CheckState.Unchecked;
                    }
                    break;

                case enumWindowType.CSharpSource:
                    if (CSharpSourceFM.IsHidden)
                    {
                        CSharpSourceFM.Show(dockPanelMain);
                        menuItemCSharpWindow.CheckState = CheckState.Checked;
                    }
                    else
                    {
                        CSharpSourceFM.Hide();
                        menuItemCSharpWindow.CheckState = CheckState.Unchecked;
                    }
                    break;

                case enumWindowType.CSharpDesignSource:
                    if (CSharpSourceDG.IsHidden)
                    {
                        CSharpSourceDG.Show(dockPanelMain);
                        menuItemCSharpDesign.CheckState = CheckState.Checked;
                    }
                    else
                    {
                        CSharpSourceDG.Hide();
                        menuItemCSharpDesign.CheckState = CheckState.Unchecked;
                    }
                    break;

                case enumWindowType.XmlSource:
                    if (xmlSourceFM.IsHidden)
                    {
                        xmlSourceFM.Show(dockPanelMain);
                        menuItemXMLWindow.CheckState = CheckState.Checked;
                    }
                    else
                    {
                        xmlSourceFM.Hide();
                        menuItemXMLWindow.CheckState = CheckState.Unchecked;
                    }
                    break;

                case enumWindowType.Desiger:
                    if (designFM.IsHidden)
                    {
                        designFM.Show(dockPanelMain);
                        menuItemDesignWindow.CheckState = CheckState.Checked;
                    }
                    else
                    {
                        designFM.Hide();
                        menuItemDesignWindow.CheckState = CheckState.Unchecked;
                    }
                    break;

                case enumWindowType.ServerExplorer:
                    if (serverExplorerFM.IsHidden)
                    {
                        serverExplorerFM.Show(dockPanelMain);
                        menuItemServerExplorer.CheckState = CheckState.Checked;
                    }
                    else
                    {
                        serverExplorerFM.Hide();
                        menuItemServerExplorer.CheckState = CheckState.Unchecked;
                    }
                    break;

                case enumWindowType.ToolStrip:
                    if (!toolStripView.Visible)
                    {
                        toolStripView.Visible = true;
                        toolStripTool.Visible = true;
                        menuItemToolStrip.CheckState = CheckState.Checked;
                    }
                    else
                    {
                        toolStripView.Visible = false;
                        toolStripTool.Visible = false;
                        menuItemToolStrip.CheckState = CheckState.Unchecked;
                    }
                    break;
            }
        }


        #endregion


         #region 控件工具栏

        /// <summary>
        /// 遍历工具栏查找所需要的控件的信息
        /// </summary>
        private void GetToolInfoAndAddToTree()
        {
            bool IsJump = false;//用于跳出多层循环
            foreach (exFullInfoForBuildChild child in exInfo.list)
            {
                for (int i = 0; i < conSideBar.Toolbox.Categories.Count; i++)
                {
                    if (IsJump)
                    {
                        IsJump = false;
                        break;
                    }
                    for (int j = 0; j < conSideBar.Toolbox.Categories[i].Items.Count; j++)
                    {
                        if (child.ControlName == conSideBar.Toolbox.Categories[i].Items[j].Name)
                        {
                            ToolboxItem tbi = (ToolboxItem)conSideBar.Toolbox.Categories[i].Items[j].Tag;
                            designFM.AddControlsToTree(tbi, child.BuildName, child.FieldName, child.ControlClass,child.LayoutText);
                            if (i == conSideBar.Toolbox.Categories.Count - 1)
                            {
                                IsJump = false;
                            }
                            else
                            {
                                IsJump = true;
                            }
                            break;
                        }
                    }
                }

            }
        }

        /// <summary>
        /// 为侧边控件工具栏绑定双击事件
        /// </summary>
        private void SidebarEventBind()
        {
            try
            {
                conSideBar.Toolbox.ItemDoubleClicked += delegate(object sender, PPCodes.ToolBoxPad.ToolBoxItem newItem)
                {
                    if (!designFM.IsHide())
                    {
                        System.Drawing.Design.ToolboxItem toolboxItem = newItem.Tag as System.Drawing.Design.ToolboxItem;

                        if (toolboxItem == null) return;
                        string preFix = null;
                        switch (toolboxItem.Description)
                        {
                            case "Win": preFix = designFM.GetPrefixOfControl(toolboxItem.DisplayName, "WinForm");
                                break;
                            case "Dev": preFix = designFM.GetPrefixOfControl(toolboxItem.DisplayName, "DevExpress");
                                break;
                            case "Web": preFix = designFM.GetPrefixOfControl(toolboxItem.DisplayName, "WebForm");
                                break;
                        }

                        designFM.AddControlsToTree(toolboxItem, preFix, "", toolboxItem.Description, preFix);//toolboxItem.Description保存了控件的类型（Win,Dev,Web）
                    }
                };
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #endregion



        /// <summary>
        /// 增加控件到列表里
        /// </summary>
        private void AddControlToTreeRefreshView(bool IsBuildLayout)
        {
            if (exInfo == null)
                return;
            CreateBuildFromBuild(exInfo.BaseControlType);
            GetToolInfoAndAddToTree();
            if (IsBuildLayout)
            {
                CreateLayoutToLayoutTree();
            }
        }

        private void CreateLayoutToLayoutTree()
        {
            designFM.CreateNewLayoutForBuild(exInfo);
        }

        /// <summary>
        /// 
        /// </summary>
        private void CreateNewProject(bool IsNew)
        {
            if (IsNew)
            {
                newProjectFM.Text = "新工程";
            }
            else
            {
                newProjectFM.Text="保存工程";
            }
            newProjectFM.ShowDialog();
            pathProject = PubConstant.GetProjectPath()+newProjectFM.GetProjectName()+"\\";
        }

        /// <summary>
        /// 新工程(判断是否需要储存)
        /// </summary>
        /// <param name="baseType"></param>
        private void CreateBuild(enumBaseControlType baseType)
        {
           if (IsNewBuildTask())
            {
                CreateNewBuild(baseType);
            }
            else
            {
               ToSaveOrNotOldBuild(baseType);
            }
        }


        /// <summary>
        /// 判断是否是新的生成
        /// </summary>
        /// <returns></returns>
        private bool IsNewBuildTask()
        {
            if (designFM.treeBaseControl.Nodes.Count < 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 开始一个新的生成
        /// </summary>
        /// <param name="baseType"></param>
        private void CreateNewBuild(enumBaseControlType baseType)
        {
            CreateNewProject(true);
            conSideBar.Show();
            designFM.SetDesignAreaHide(false);
            designFM.AddBaseControlToTree(baseType);
        }


        private void SaveOldBuild()
        {
            try
            {
                if (String.IsNullOrEmpty(pathProject))
                {
                    CreateNewProject(false);
                }
                designFM.BuildXMLCode(xmlSourceFM, pathProject);
                designFM.BuildCSharpCode(CSharpSourceFM, CSharpSourceDG, pathProject);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void CleanAllInfo()
        {
           designFM.CleanAllTree();
           CSharpSourceDG.CleanText();
           CSharpSourceFM.CleanText();
           xmlSourceFM.CleanText();
        }

        /// <summary>
        /// 是否保存旧的生成，新建一个新的生成
        /// </summary>
        /// <param name="baseType"></param>
        private void ToSaveOrNotOldBuild(enumBaseControlType baseType)
        {
            switch (MessageBox.Show("现有的设计未保存，是否保存？", "未保存设计", MessageBoxButtons.YesNoCancel))
            {
                case DialogResult.Yes:
                    SaveOldBuild();
                    CleanAllInfo();
                    CreateNewBuild(baseType);
                    break;
                case DialogResult.No:
                    CleanAllInfo();
                    CreateNewBuild(baseType);
                    break;
                case DialogResult.Cancel:
                    //return false;
                    break;
            }
        }


        /// <summary>
        /// 新工程(判断是否需要储存)
        /// </summary>
        /// <param name="baseType"></param>
        private void CreateBuildFromBuild(enumBaseControlType baseType)
        {
            if (IsNewBuildTask())
            {
                CreateNewBuild(baseType);
            }
            else
            {
                ToSaveOrNotOldBuild(baseType);
            }
        }


        #region OLD注释


        ///// <summary>
        ///// 开始一个新的生成
        ///// </summary>
        ///// <param name="baseType"></param>
        //private void CreateNewBuild(enumBaseControlType baseType, string BaseBuildName)
        //{
        //    conSideBar.Show();
        //    designFM.SetDesignAreaHide(false);
        //    designFM.AddBaseControlToTree(baseType, BaseBuildName);
        //}        


        ///// <summary>
        ///// 是否保存旧的生成，新建一个新的生成
        ///// </summary>
        ///// <param name="baseType"></param>
        //private void ToSaveOrNotOldBuild(enumBaseControlType baseType)
        //{
        //  switch (MessageBox.Show("现有的设计未保存，是否保存？", "未保存设计", MessageBoxButtons.YesNoCancel))
        //    {
        //        case DialogResult.Yes:
        //            //Save(false);
        //            designFM.CleanAllTree();
        //            CreateNewBuild(baseType, baseType.ToString());
        //            break;
        //        case DialogResult.No:
        //            designFM.CleanAllTree();
        //            CreateNewBuild(baseType, baseType.ToString());
        //            break;
        //        case DialogResult.Cancel:
        //            //return false;
        //            break;
        //    }
        //}

        #endregion
        
        #endregion


        #region 公共方法外部可调用

        /// <summary>
        /// 供外部调用，获取用来生成的所有信息
        /// </summary>
        /// <param name="_exInfo"></param>
        public void AddBuildInfoToTree(exFullInfoForBuild _exInfo, bool IsBuildLayout)
        {
            exInfo = _exInfo;
            AddControlToTreeRefreshView(IsBuildLayout);
        }


        #endregion


        #region 主窗体事件
		
        private void MainStudio_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveMainWindowDockingConfig();
        }

	    #endregion


        #region 工具条事件

        #region View工具条
   
        private void tsBtnSerE_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.ServerExplorer);
        }

        private void tsBtnDesign_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.Desiger);
        }

        private void tsBtnXMLCode_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.XmlSource);
        }

        private void tsBtnCSharp_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.CSharpSource);
        }

        private void tsBtnCSharpDesign_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.CSharpDesignSource);
        } 
        #endregion


        #region Tool工具条
       
        /// <summary>
        /// 生成代码
        /// 1：首先生成XML代码
        /// 2：其次生成C#代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsBtnBuildXMLCode_Click(object sender, EventArgs e)
        {
            try
            {
                int num = designFM.IsAllControlInLayout();
                switch (num)
                {
                    case 1:
                        MessageBox.Show("含有空的Item，这些Item将自动忽略并不生成！");
                        designFM.BuildXMLCode(xmlSourceFM, pathProject);
                        break;
                    case 2:
                        MessageBox.Show("不是所有控件都在布局中，请确保所有控件都在布局中，请返回修改！");
                        break;
                    case 3:
                        MessageBox.Show("含有空的Group，生成时将出错，请返回删除！");
                        break;
                    default:
                        designFM.BuildXMLCode(xmlSourceFM, pathProject);
                        break;
                }
                //MessageBox.Show("生成成功！");     
                //string path = PubConstant.ControlXmlTemplatePath;
                //path = String.Format("{0}BuildTest2.xml", path);

                //xmlSourceFM.SetText(path);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// 生成C#代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsBtnBuildCSharpCode_Click(object sender, EventArgs e)
        {
            try
            {
                int num = designFM.IsAllControlInLayout();
                switch (num)
                {
                    case 1:
                        MessageBox.Show("含有空的Item，这些Item将自动忽略并不生成！");
                        designFM.BuildCSharpCode(CSharpSourceFM, CSharpSourceDG, pathProject);
                        break;
                    case 2:
                        MessageBox.Show("不是所有控件都在布局中，请确保所有控件都在布局中，请返回修改！");
                        break;
                    case 3:
                        MessageBox.Show("含有空的Group，生成时将出错，请返回删除！");
                        break;
                    default:
                        designFM.BuildCSharpCode(CSharpSourceFM, CSharpSourceDG, pathProject);
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }


        /// <summary>
        /// 生成全部
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsBtnBuildAll_Click(object sender, EventArgs e)
        {
            int num = designFM.IsAllControlInLayout();
            switch (num)
            {
                case 1:
                    MessageBox.Show("含有空的Item，这些Item将自动忽略并不生成！");
                    designFM.BuildXMLCode(xmlSourceFM, pathProject);
                    designFM.BuildCSharpCode(CSharpSourceFM, CSharpSourceDG, pathProject);
                    break;
                case 2:
                    MessageBox.Show("不是所有控件都在布局中，请确保所有控件都在布局中，请返回修改！");
                    break;
                case 3:
                    MessageBox.Show("含有空的Group，生成时将出错，请返回删除！");
                    break;
                default:
                    designFM.BuildXMLCode(xmlSourceFM, pathProject);
                    designFM.BuildCSharpCode(CSharpSourceFM, CSharpSourceDG, pathProject);
                    break;
            }
        }


        /// <summary>
        /// 代码工厂
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsBtnCodeFactory_Click(object sender, EventArgs e)
        {
            CodeShop cf = new CodeShop();
            cf.Show();
        }

        
        #endregion


        #endregion


        #region 菜单栏事件

        #region View


        private void menuItemControlsSideBar_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.ControlSideBar);
        }

        private void menuItemCSharpWindow_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.CSharpSource);
        }

        private void menuItemCSharpDesign_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.CSharpDesignSource);
        }

        private void menuItemXMLWindow_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.XmlSource);
        }

        private void menuItemDesignWindow_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.Desiger);
        }

        private void menuItemToolStrip_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.ToolStrip);
        }

        private void menuItemServerExplorer_Click(object sender, EventArgs e)
        {
            SetWindowShowOrHideState(enumWindowType.ServerExplorer);
        }

        private void menuItemProjectExplorer_Click(object sender, EventArgs e)
        {
            //if (designFM.IsHidden)
            //{
            //    designFM.Show(dockPanelMain);
            //    menuItemDesignWindow.CheckState = CheckState.Checked;
            //}
            //else
            //{
            //    designFM.Hide();
            //    menuItemDesignWindow.CheckState = CheckState.Unchecked;
            //}
        }


        #endregion


        #region File

        private void menuItemExitNoSave_Click(object sender, EventArgs e)
        {
            m_bSaveLayout = false;
            this.Close();
            m_bSaveLayout = true;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemClose_Click(object sender, EventArgs e)
        {
            /// Try to close our form. Our OnClosing method will check to make sure our stuff
            /// is saved before closing.
            //if (designFM.DestroyDesigner())
            //   {
            SaveMainWindowDockingConfig();
            this.Close();
            //}
        }

        /// <summary>
        /// 新建工程(默认为新建一个DevUserControl生成)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemNew_Click(object sender, EventArgs e)
        {
            //CreateBuild(enumBaseControlType.DevUserControl);
        }

        /// <summary>
        /// 新建DevUserControl工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemDevCont_Click(object sender, EventArgs e)
        {
            CreateBuild(enumBaseControlType.XtraUserControl);
        }

        /// <summary>
        /// 新建DevForm工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemDevForm_Click(object sender, EventArgs e)
        {
            CreateBuild(enumBaseControlType.XtraForm);
        }

        /// <summary>
        /// 新建UserControl工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemUserCont_Click(object sender, EventArgs e)
        {
            CreateBuild(enumBaseControlType.UserControl);
        }

        /// <summary>
        /// 新建Form工程
        /// </summary>    
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemForm_Click(object sender, EventArgs e)
        {
            CreateBuild(enumBaseControlType.Form);
        }

        /// <summary>
        /// 新建DevRibbonForm工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItemDevRibon_Click(object sender, EventArgs e)
        {
            CreateBuild(enumBaseControlType.RibbonForm);
        }

        #region OLD

        ///// <summary>
        ///// 打开一个XML文件用于生成
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void menuItemOpen_Click(object sender, EventArgs e)
        //{
        //    //if (designFM.DestroyDesigner()) // make sure we're clear for a new designer
        //    //{
        //    //    if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
        //    //    {
        //    //        // Load up this file (XML is all we support for now).
        //    //        SampleDesignerLoader designerLoader = new SampleDesignerLoader(openFileDialog1.FileName);
        //    //        designFM.CreateDesigner(designerLoader);
        //    //    }
        //    //}
        //}

        ///// The user gets to choose between saving in XML, C#, or VB.
        ///// Just remember that you can only load XML.
        //private void menuItemSave_Click(object sender, EventArgs e)
        //{
        //    //designFM.loader.Save(false); // false == don't force file dialog if file already chosen

        //}


        ///// The user gets to choose between saving in XML, C#, or VB.
        ///// Just remember that you can only load XML.
        //private void menuItemSaveAs_Click(object sender, EventArgs e)
        //{
        //    //designFM.loader.Save(true); // true == force file dialog even if file already chosen

        //}



        ///// The loader also handles building. It keeps a codeCompileUnit that represents
        ///// the state of the document at any time. The user will be prompted for an executable
        ///// filename the first time there's a build.
        //private void menuItemBuild_Click(object sender, EventArgs e)
        //{
        //    //designFM.loader.Build();
        //}

        ///// This builds to the executable the user has selected and then runs it.
        //private void menuItemRun_Click(object sender, EventArgs e)
        //{
        //    //designFM.loader.Run();
        //}

        ///// Another way to kill any running executable.
        //private void menuItemStop_Click(object sender, EventArgs e)
        //{
        //    //designFM.loader.Stop();
        //}

        #endregion
        #endregion


        #region Tools

        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsMenuItemOptions_Click(object sender, EventArgs e)
        {
            using (Options o = new Options())
            {
                o.ShowDialog();
            }                       
        }
        
        /// <summary>
        /// 代码工厂
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsMICodeFactory_Click(object sender, EventArgs e)
        {
            CodeShop cf=new CodeShop();
            cf.Show();
        }

        /// <summary>
        /// 配置管理器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsMIConfigManager_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("ConfigManager.exe");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 配置管理器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsBtnConfigManager_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("ConfigManager.exe");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion


        #region Build
		
        private void menuItemBuild_Click(object sender, EventArgs e)
        {
            int num = designFM.IsAllControlInLayout();
            switch (num)
            {
                case 1:
                    MessageBox.Show("含有空的Item，这些Item将自动忽略并不生成！");
                    designFM.BuildXMLCode(xmlSourceFM,pathProject);
                    designFM.BuildCSharpCode(CSharpSourceFM, CSharpSourceDG,pathProject);
                    break;
                case 2:
                    MessageBox.Show("不是所有控件都在布局中，请确保所有控件都在布局中，请返回修改！");
                    break;
                case 3:
                    MessageBox.Show("含有空的Group，生成时将出错，请返回删除！");
                    break;
                default:
                    designFM.BuildXMLCode(xmlSourceFM,pathProject);
                    designFM.BuildCSharpCode(CSharpSourceFM, CSharpSourceDG,pathProject);
                    break;
            }
        }    
        

	#endregion


        #region Help
           
        private void tsMIAbout_Click(object sender, EventArgs e)
        {
            using (AboutBox ab = new AboutBox())
            {
                ab.ShowDialog();
            }
        }
        
        #endregion

    


 



    
        #endregion







    }
}
